package com.codemeta.bokit.flowable.config;

import com.codemeta.bokit.props.FlowableProps;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * 流程引擎配置文件
 *
 * @author hice6
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(FlowableProps.class)
public class ProcessEngineConfig {

    /**
     * 初始化流程引擎
     *
     * @return .
     */
    @Bean
    public ProcessEngine processEngine(FlowableProps flowableProps) {
        // 流程引擎配置
        ProcessEngineConfiguration cfg = null;
        try {
            cfg = new StandaloneProcessEngineConfiguration()
                    .setJdbcUrl(flowableProps.getUrl())
                    .setJdbcUsername(flowableProps.getUsername())
                    .setJdbcPassword(flowableProps.getPassword())
                    .setJdbcDriver(flowableProps.getDriverClassName())
                    .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
                    .setMailServerHost(flowableProps.getMailServerHost())
                    .setMailServerPassword(flowableProps.getMailServerPassword())
                    .setMailServerDefaultFrom(flowableProps.getMailServerDefaultFrom())
                    .setMailServerUsername(flowableProps.getMailServerUsername())
                    .setActivityFontName("宋体")
                    .setLabelFontName("宋体")
                    .setAnnotationFontName("宋体");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 初始化流程引擎对象
        return Objects.requireNonNull(cfg).buildProcessEngine();
    }

}